package yl.hs.bmipfull.service.salemanage.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.salemanage.ILinkerCommunicateRecordDao;
import yl.hs.bmipfull.pojo.salemanage.LinkerCommunicateRecordEntity;
import yl.hs.bmipfull.service.salemanage.ILinkerCommunicateRecordService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.utils.datastate.RelationType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * 作用:  联系人沟通记录服务层接口实现<br/>
 * 创建时间: 2021年05月19日 19时17分30秒 <br/>
 *
 * @author 谢东
 */
@Service
public class LinkerCommunicateRecordServiceImpl extends SaleBaseService implements ILinkerCommunicateRecordService {

    @Autowired
    ILinkerCommunicateRecordDao dao;

    @Override
    public List<LinkerCommunicateRecordEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(LinkerCommunicateRecordEntity.class);
        return dao.queryList(pageQuery.getQuery());
    }

    @Override
    public PageResult<LinkerCommunicateRecordEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(LinkerCommunicateRecordEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<LinkerCommunicateRecordEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }


    @Override
    public LinkerCommunicateRecordEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional
    public Integer saveLinkerCommunicateRecord(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;

        if (Tl.isEmpty(sn)) {
            //执行保存
            LinkerCommunicateRecordEntity entity = new LinkerCommunicateRecordEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(LinkerCommunicateRecordEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeUserTag(entity.getSn(), entity.getClass(), RelationType.CREATE.toString());
                writeLog(ActionType.CREATE, entity.getSn(), "插入联系人沟通记录信息", Tl.toJson(entity), LinkerCommunicateRecordEntity.class);
            }

        } else {
            //执行修改
            var entity = findBySn(sn);
            var backJson = entity.toJson();
            var changeLog = Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, backJson, changeLog, LinkerCommunicateRecordEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional
    public Integer delete(String[] sn) {

        return 0;
    }
}